<?php
/**
 * Created by PhpStorm.
 * User: work
 * Date: 18-8-11
 * Time: 下午3:00
 */

namespace PddWechat\Models\Admin\Service;


use Dai\Framework\Library\Log;
use PddWechat\Models\User\Data\UserData;

class UserList
{
    /**
     * @param $basePageInfo
     * @return array
     */
    public function execute($basePageInfo) {
        /** @var BaseSessionInfo $sessionData */
        $sessionData = $basePageInfo->sessionInfo;

        if( $sessionData->adminLevel !== 10) {
            header("/mall/index");
            exit;
        }
        $params = $basePageInfo->params;

        $userData = new UserData();
        $list = $userData->getList($params->pn, $params->rn);
        if( ! empty($list)) {
            $uids = [];
            foreach ($list as $item) {
                $uids[] = intval($item->inviteUid);
            }

            $userData = new UserData();
            $userDos = $userData->selectByUids( array_unique($uids) );
            foreach ($list as &$item) {
                $item->inviteUserName = $userDos[$item->inviteUid]->userName;
            }
        }

        Log::info("list is %s ",var_export($list, true));

        $output = [
            'list' => $list,
            'total' => $userData->total(),
            'nextUrl' => '/admin/user/list?pn=' .($params->pn + $params->rn),
            'preUrl' => '/admin/user/list?pn=' .($params->pn - $params->rn > 0 ? $params->pn - $params->rn : 0 ),
        ];
        return $output;
    }
}